<?php

namespace app\common\logic;

use think\Model;

class Department extends Model
{
    /**
     * 批量插入
     * @param $list
     * @return array|false
     * @author wb <pithyone@vip.qq.com>
     */
    public function addList($list)
    {
        $ret = $this->saveAll($list, false);
        return $ret;
    }

    public function addItem($data)
    {
        $ret = $this->allowField(true)->isUpdate(false)->save($data);
        return $ret;
    }

    /**
     * 查询树
     * @param $pid
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getTreeByPid($pid)
    {
        $lists = self::where('')->field('id,name text,parentid')->order("`code` asc")->select();

        if (empty($lists))
            return [];

        $info = self::where('')->field('id,name text,parentid')->find($pid);

        $ret = $info;
        $ret['state'] = ['selected' => true, 'opened' => true];
        $ret['children'] = self::toTree($lists, $pid);

        return $ret;
    }

    /**
     * 生成树
     * @param $lists
     * @param int $parentId
     * @return array
     * @author wb <pithyone@vip.qq.com>
     */
    private static function toTree($lists, $parentId = 0)
    {
        $ret = [];

        foreach ($lists as $list) {
            if ($list['parentid'] == $parentId) {

                $child = self::toTree($lists, $list['id']);
                if ($child) {
                    $list['children'] = $child;
                }

                $ret[] = $list;
//                unset($lists[$list['id']]);
            }
        }
        return $ret;
    }

    private static function toIdTree($lists, $parentId = 0)
    {
        $ret = [];

        foreach ($lists as $list) {
            if ($list['parentid'] == $parentId) {

                $child = self::toIdTree($lists, $list['id']);
                if (!empty($child))
                    $ret = array_merge($ret, $child);

                $ret[] = $list;
//                unset($lists[$list['id']]);
            }
        }
        return $ret;
    }

    public static function getChildIds($id)
    {
        //获取部门列表
        $dpts = self::all();
        $trees = self::toIdTree($dpts, $id);
        $ids[] = $id;
        foreach ($trees as $k => $val) {
            $ids[] = $val['id'];
        }

        return $ids;
    }

    /**
     * 获取顶级部门
     * @param $id
     * @return int
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getPidById($id)
    {
        $dpts = self::where('')->select();
        $pid = self::_pidDptTree($dpts, $id);

        return $pid;
    }

    /**
     * 递归父级
     * @param $lists
     * @param int $id
     * @return int
     * @author wb <pithyone@vip.qq.com>
     */
    private static function _pidDptTree($lists, $id = 0)
    {
        foreach ($lists as $list) {
            if ($list['id'] == $id) {

                if ($list['parentid'] == 1)
                    return $list['id'];

                return self::_pidDptTree($lists, $list['parentid']);
            }
        }
        return 0;
    }
}